package com.dorm.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dorm.common.PageResult;
import com.dorm.common.Result;
import com.dorm.entity.Building;
import com.dorm.service.BuildingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 宿舍楼控制器
 */
@RestController
@RequestMapping("/building")
public class BuildingController {

    @Autowired
    private BuildingService buildingService;

    /**
     * 分页查询
     */
    @GetMapping("/page")
    public Result<PageResult<Building>> page(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) String buildingNo,
            @RequestParam(required = false) String buildingName) {
        
        Page<Building> page = buildingService.pageQuery(pageNum, pageSize, buildingNo, buildingName);
        PageResult<Building> result = new PageResult<>(
                page.getRecords(),
                page.getTotal(),
                page.getCurrent(),
                page.getSize()
        );
        return Result.success(result);
    }

    /**
     * 根据ID查询
     */
    @GetMapping("/{id}")
    public Result<Building> getById(@PathVariable Long id) {
        Building building = buildingService.getById(id);
        return Result.success(building);
    }

    /**
     * 新增
     */
    @PostMapping
    public Result<String> add(@RequestBody Building building) {
        buildingService.save(building);
        return Result.success("添加成功");
    }

    /**
     * 修改
     */
    @PutMapping
    public Result<String> update(@RequestBody Building building) {
        buildingService.updateById(building);
        return Result.success("修改成功");
    }

    /**
     * 删除
     */
    @DeleteMapping("/{id}")
    public Result<String> delete(@PathVariable Long id) {
        buildingService.removeById(id);
        return Result.success("删除成功");
    }

    /**
     * 查询所有
     */
    @GetMapping("/list")
    public Result<List<Building>> list() {
        List<Building> list = buildingService.list();
        return Result.success(list);
    }
}

